package com.dfxs.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhangdongsheng
 * @version 1.0
 * @describe 创建5个线程,保证按照创建顺序执行
 * @date 2022/2/17 22:57
 */
public class ThreadTest1 {
    static ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static void main(String[] args) throws InterruptedException {
        test1();
        System.out.println("-----------------------------");
        test2();
    }

    public static void test2(){
        ThreadDemo threadDemo1 = new ThreadDemo(1);
        ThreadDemo threadDemo2 = new ThreadDemo(2);
        ThreadDemo threadDemo3 = new ThreadDemo(3);
        ThreadDemo threadDemo4 = new ThreadDemo(4);
        ThreadDemo threadDemo5 = new ThreadDemo(5);
        executorService.execute(threadDemo1);
        executorService.execute(threadDemo2);
        executorService.execute(threadDemo3);
        executorService.execute(threadDemo4);
        executorService.execute(threadDemo5);
        executorService.shutdown();
    }

    public static void test1() throws InterruptedException {
        ThreadDemo threadDemo1 = new ThreadDemo(1);
        ThreadDemo threadDemo2 = new ThreadDemo(2);
        ThreadDemo threadDemo3 = new ThreadDemo(3);
        ThreadDemo threadDemo4 = new ThreadDemo(4);
        ThreadDemo threadDemo5 = new ThreadDemo(5);

        threadDemo1.start();
        threadDemo1.join();

        threadDemo2.start();
        threadDemo2.join();

        threadDemo3.start();
        threadDemo3.join();

        threadDemo4.start();
        threadDemo4.join();

        threadDemo5.start();
        threadDemo5.join();
    }

    static class ThreadDemo extends Thread{
        int num;
        public ThreadDemo(int num){
            this.num = num;
        }
        @Override
        public void run() {
//            System.out.println("thread"+num);
            System.out.println(Thread.currentThread().getName());
        }
    }
}
